<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['params']=array();
$merchid=$this->checklogin();
$return['status']=$this->order_status;
$return['buytype']=$this->buy_types;
$return['sendtype']=$this->send_types;
$return['paytype']=get_pay_types();
$return['paytype']['peerpay']='找人代付';

$return['ispay']=array('0'=>'未支付','1'=>'已支付');
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'clearstatus'=>1);
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;	
    $orderby='createtime desc';
	$total='';
	if(!empty($_RQ['orderid'])){
		$params['orderid']=trim($_RQ['orderid']);
		$return['params']['orderid']=$_RQ['orderid'];
	}
	if(!empty($_RQ['ordersn'])){
		$orderid=pdo_getcolumn('jy_weishop_order',array('ordersn'=>$_RQ['ordersn']),'id');
		$params['orderid']=$orderid;
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$return['list']=pdo_getslice('jy_weishop_order_goods',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$gv){
			$order=pdo_get('jy_weishop_order',array('id'=>$gv['orderid']),array('ordersn','createtime'));
			$gv['ordersn']=$order['ordersn'];
			$gv['createtime']=$order['createtime'];
			$gv['ordergoodsid']=$gv['id'];
			$gv['id']=$gv['orderid'];
			$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','image','title','merchid'));
			$gv['image']=$goodsinfo['image'];
			$gv['title']=$goodsinfo['title'];
			//商品规格
			if($gv['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$optionid),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$goodsinfo['selespename']=implode("，",$specs);
			}
			if(!$gv['fee_desc']){
				model('jy_weishop/order')->setfee_desc($gv['orderid']);
				$gv['total_price']=pdo_getcolumn('jy_weishop_order_goods',array('id'=>$gv['ordergoodsid']),'total_price');
			}
			//计算佣金
			if($this->plugin_exists('jy_commission')){
				$gv['commission_money']=model('jy_commission/commission')->get_goodsfee($gv['goodsid'],$gv['orderid'],$gv['ordergoodsid']);
			}
			$gv['merchorder']=$this->model('commission')->get_choufee($gv['orderid'],true,$merchid);
			$gv['merchorder_commission_money']=$gv['merchorder']['commission_money']?:0;
			if($gv['merchorder']['status']){
				$gv['merchorder_statustitle']='可结算';
			}else{
				$gv['merchorder_statustitle']=$gv['merchorder']['commission_money']?'不可结算':'未成交';
			}
	}
	$htmls=array(
		'type'=>'list',
		//'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'已结算订单/商品'),
		),
		'list'=>array('data'=>'list','tip'=>'[<red>温馨提示</red>]：基本设置--商户的可提现金额是否扣除分销佣金，开启后，结算才会扣除【分销佣金】','delall'=>false,'del_one'=>false,'edit'=>false,'list'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'total_price','title'=>'金额','type'=>'string'),
			array('name'=>'commission_money','title'=>'需扣除分销佣金','type'=>'string'),
			array('type'=>'img_string','title'=>'平台抽成情况',
			'data'=>array(
				array('title'=>'金额：￥','name'=>'merchorder_commission_money'),
				array('title'=>'状态：','name'=>'merchorder_statustitle')
				)
			),
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),		
		),
		'action'=>array(	
			array('type'=>'href','action'=>'manage.order_detail/','title'=>'订单详情'),		
		),),
		
		'search'=>array(
			array('name'=>'ordersn','width'=>15,'title'=>'订单号','type'=>'string'),
			array('name'=>'createtime','width'=>15,'title'=>'下单时间','type'=>'time_range'),
		)
	);
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('manage/order_index');
	}else{
		$return['htmls']=$htmls;
	}
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>3);
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;	
    $orderby='createtime desc';
	$total='';
	if(!empty($_RQ['orderid'])){
		$params['orderid']=trim($_RQ['orderid']);
		$return['params']['orderid']=$_RQ['orderid'];
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	if($_RQ['postdata']){
		$postdata=get_postdata();
		
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$ids=pdo_getall('jy_weishop_order',$params,'id','id');
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'clearstatus'=>0);
	if($ids){
		$ids=array_keys($ids);
		$params['orderid']=$ids;
	}
	$list=pdo_getslice('jy_weishop_order_goods',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as &$gv){
			$order=pdo_get('jy_weishop_order',array('id'=>$gv['orderid']),array('ordersn','createtime'));
			$gv['ordersn']=$order['ordersn'];
			$gv['createtime']=$order['createtime'];
			$gv['ordergoodsid']=$gv['id'];
			$gv['id']=$gv['orderid'];
			$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','image','title','merchid'));
			$gv['image']=$goodsinfo['image'];
			$gv['title']=$goodsinfo['title'];
			//商品规格
			if($gv['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$optionid),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$goodsinfo['selespename']=implode("，",$specs);
			}
			if(!$gv['fee_desc']){
						model('jy_weishop/order')->setfee_desc($gv['orderid']);
						$gv['total_price']=pdo_getcolumn('jy_weishop_order_goods',array('id'=>$gv['ordergoodsid']),'total_price');
			}
			//计算佣金
			if($this->plugin_exists('jy_commission')){
					$gv['commission_money']=model('jy_commission/commission')->get_goodsfee($gv['goodsid'],$gv['orderid'],$gv['ordergoodsid']);
			}
			$gv['merchorder']=$this->model('commission')->get_choufee($gv['orderid'],true,$gv['merchid']);
			$gv['merchorder_commission_money']=$gv['merchorder']['commission_money']?:0;
			if($gv['merchorder']['status']){
				$gv['merchorder_statustitle']='可结算';
			}else{
				$gv['merchorder_statustitle']=$gv['merchorder']['commission_money']?'不可结算':'未成交';
			}
			$gv['createtime']=date('Y-m-d H:i:s',$gv['createtime']);
	}
	$header=array(
		'ordersn'=>'订单号',
		'total_price'=>'金额',
		'commission_money'=>'需扣除佣金',
		'merchorder_commission_money'=>'抽成',
		'merchorder_statustitle'=>'状态',	
		'createtime'=>'下单时间',
	);
	cfc('tools')->export2excel($header,$list,'订单');
}

?>